import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User, UserDto } from '../../entities';

interface Result<T> {
  success: boolean;
  value: T;
}

@Injectable()
export class UsersService {

  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>,
  ) {}

  /**
   * @function 查询所有用户
   */
  async findAll(): Promise<Result<User[]>> {
    const users = await this.userRepository.find();
    return { success: true, value: users };
  }

  /**
   * @function 根据邮箱查询用户
   */
  async findOne(email: string): Promise<Result<User>> {
    const user = await this.userRepository.findOne(email);
    return { success: true, value: user };
  }

  /**
   * @function 根据uid查询用户
   */
  async findOneById(uid: string): Promise<Result<User>> {
    const user = await this.userRepository.findOne(uid);
    return { success: true, value: user };
  }

  /**
   * 根据uid数组查询用户
   * @params uid[]
   */
  async findUsersByIds(uid: any[]): Promise<Result<User[]>> {
    const users = await this.userRepository.findByIds(uid);
    return { success: true, value: users };
  }

}
